package lium.lbs.lbsim.fragments

import android.view.KeyEvent
import android.view.View
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.FrameLayout
import com.buz.hjcdriver.bean.WebViewResultBean
import com.lmlibrary.bean.ResponseBean
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.fragment_found.*
import lium.lbs.lbsim.R
import lium.lbs.lbsim.jgchat.utils.photovideo.takevideo.utils.LogUtils
import lium.lbs.lbsim.utils.callbck.DialogCallback
import java.util.HashMap

class FoundFragment : BaseFragment() {
    //定义变量
    private var mWebView: WebView? = null

    override fun getLayoutId(): Int {
        return R.layout.fragment_found
    }

    companion object {
        fun newInstance(): FoundFragment {
            return FoundFragment()
        }
    }

    override fun onClick(v: View?) {
    }

    override fun initView() {
        val params =
            FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)
        mWebView = WebView(activity)
        web_view_container.addView(mWebView, params)
        var webSettings = mWebView!!.settings
        webSettings.javaScriptEnabled = true
        webSettings.javaScriptCanOpenWindowsAutomatically = true
        webSettings.allowFileAccess = true// 设置允许访问文件数据
        webSettings.setSupportZoom(true)//支持缩放
        webSettings.javaScriptCanOpenWindowsAutomatically = true
        webSettings.cacheMode = WebSettings.LOAD_NO_CACHE
        webSettings.domStorageEnabled = true
        webSettings.databaseEnabled = true
        mWebView!!.setOnKeyListener(OnKeyEvent)
        mWebView!!.setWebViewClient(webClient)
        getDataList()
    }


    private val webClient = object : WebViewClient() {
        override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
            LogUtils.d("TAG" + url)
            return false
        }
    }

    private val OnKeyEvent = View.OnKeyListener { v, keyCode, event ->
        val action = event.action
        val webView = v as WebView
        if (KeyEvent.ACTION_DOWN == action && KeyEvent.KEYCODE_BACK == keyCode) {
            if (webView?.canGoBack()) {
                webView.goBack()
                return@OnKeyListener true
            }
        }
        false
    }


    override fun onResume() {
        super.onResume()
        mWebView?.onResume()
    }

    override fun onPause() {
        super.onPause()
        mWebView?.onPause()
    }

    override fun onDestroy() {
        super.onDestroy()
        mWebView?.clearCache(true)
        (mWebView?.parent as FrameLayout).removeView(mWebView)
        mWebView?.stopLoading()
        mWebView?.setWebViewClient(null)
        mWebView?.setWebChromeClient(null)
        mWebView?.removeAllViews()
        mWebView?.destroy()
        mWebView = null
    }

    fun getDataList() {
        val param = HashMap<String, String>()
        postData(
            "/common/getUrl",
            param,
            object : DialogCallback<ResponseBean<WebViewResultBean>>(baseActivity) {
                override fun onSuccess(response: Response<ResponseBean<WebViewResultBean>>) {
                    setDataList(response.body().data)
                }
            })


    }

    fun setDataList(webViewResultBean: WebViewResultBean) {
        mWebView!!.loadUrl(webViewResultBean.content)
    }

}